package com.sgb.kjwl.view.ui.fragment.home;

import android.graphics.Rect;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.material.tabs.TabLayout;
import com.sgb.kjwl.BaseHeightViewPagerFragment;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.FragmentCentralizedMining2Binding;
import com.sgb.kjwl.di.home.DaggerHomeFragComponent;
import com.sgb.kjwl.di.home.HomeFragModule;
import com.sgb.kjwl.model.entity.BannerEntity;
import com.sgb.jicai.entity.CentralizedMiningEntity;
import com.swgk.core.view.entity.MenuEntity;
import com.swgk.core.util.PickSelectEntity;

import com.sgb.jicai.entity.FactoryCentralizedItemEntity;
import com.sgb.kjwl.view.event.CategoryEvent2;
import com.swgk.core.event.EventConstant;
import com.sgb.kjwl.view.ui.adapter.home.CentralizedMining4Adapter;
import com.sgb.jicai.view.adapter.FactoryCentralizedListItemAdapter2;
import com.sgb.kjwl.viewmodel.home.CentralizedMininglModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick3;
import com.swgk.core.util.ClickUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;


/**
 * Description:
 * Author cfk
 * Time
 * 租赁市场中的集采fragment
 */
public class CentralizedMiningFragment extends BaseHeightViewPagerFragment {

    public static final String GROUPBUY = "groupbuy";//集采
    @Inject
    CentralizedMininglModel viewModel;
    private FragmentCentralizedMining2Binding binding;
    ArrayList<MenuEntity> entitielist = new ArrayList<>();
    private int mItemType;
    int page = 1;
    private CentralizedMining4Adapter centralizedMining4Adapter;
    //供方适配器
    private FactoryCentralizedListItemAdapter2 factoryCentralizedListItemAdapter2;

    private List<PickSelectEntity> typeEntityList;
    private List<PickSelectEntity> typeTwoEntityList;
    private PickSelectEntity typeEntity;
    private PickSelectEntity typeTwoEntity;

    public static CentralizedMiningFragment newInstance(int position, BannerEntity bannerEntity, boolean isRefresh) {
        CentralizedMiningFragment fragment = new CentralizedMiningFragment();
        Bundle args = new Bundle();
        args.putSerializable(ARG_PARAM, bannerEntity);
        args.putInt(ARG_PARAM_POS, position);
        args.putBoolean(ARG_isRefresh, isRefresh);
        fragment.setArguments(args);
        return fragment;
    }

    private int getItemType() {
        int mItemType = -1;
        BannerEntity bannerEntity = (BannerEntity) getArguments().getSerializable(ARG_PARAM);
        switch (bannerEntity.getName()) {
            case "即报即采":
                mItemType = 0;
                break;
            case "汇集采购":
                mItemType = 1;
                break;
            case "企业专项":
                mItemType = 2;
                break;
            case "需方发起":
                mItemType = 3;
                break;
            case "供方发起":
                mItemType = 4;
                break;
        }
        return mItemType;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() == null) {
            return;
        }
        mItemType = getItemType();
    }

    @Override
    public void onStart() {
        super.onStart();
        if (!EventBus.getDefault().isRegistered(getActivity())) {
            EventBus.getDefault().register(getActivity());
        }
    }

    @Override
    protected void setupFragmentComponent(AppComponent appComponent) {
        DaggerHomeFragComponent.builder()
                .appComponent(appComponent)
                .homeFragModule(new HomeFragModule(this))
                .build()
                .inject(this);
    }

    public void IsRefresh(boolean isRefresh) {
        setIsRefresh(isRefresh);
        loadData(false);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_centralized_mining2, container, false);
        return binding.getRoot();
    }

    @Override
    protected void initView() {
        super.initView();
        if (getArguments() == null) {
            return;
        }
        mItemType = getItemType();
        if (mItemType == -1) {
            return;
        }
        binding.tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
        binding.recycler.setNestedScrollingEnabled(false);
        binding.recycler.setNestedScrollingEnabled(false);
        binding.recycler.setLayoutManager(new LinearLayoutManager(getContext()));
        binding.recycler.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, RecyclerView.State state) {
                super.getItemOffsets(outRect, view, parent, state);
                outRect.bottom = getResources().getDimensionPixelSize(R.dimen.dp1);
            }
        });
        centralizedMining4Adapter = new CentralizedMining4Adapter(getActivity(), null);
        centralizedMining4Adapter.setOnItemClick(new AdapterOnItemClick3<CentralizedMiningEntity>() {
            @Override
            public void onItemClick(CentralizedMiningEntity centralizedMiningEntity, int position) {
                if (!ClickUtils.isDoubleClick()) {
                    viewModel.getH5Url(centralizedMiningEntity.getNo(), "collection");
                }
            }
        });
        centralizedMining4Adapter.onIChildClick(new CentralizedMining4Adapter.IChildClick() {
            @Override
            public void onClick(CentralizedMiningEntity goodsEntity, int position) {
                if (!ClickUtils.isDoubleClick()) {
                    viewModel.getH5Url(goodsEntity.getNo(), "collection");
                }
            }
        });
        factoryCentralizedListItemAdapter2 = new FactoryCentralizedListItemAdapter2(getActivity(), null, "1");
        factoryCentralizedListItemAdapter2.setOnItemClick(new AdapterOnItemClick<FactoryCentralizedItemEntity>() {
            @Override
            public void onItemClick(FactoryCentralizedItemEntity entity, int position) {
                viewModel.getH5Url(entity.getNo(), "Factory");
            }
        });
        binding.tvMore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                loadData(true);
            }
        });
        binding.tvGoModel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EventBus.getDefault().postSticky(CategoryEvent2.make(EventConstant.EVENT_CATEGORY_CHOOSE2, 3));
            }
        });
        binding.tvGoModel1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EventBus.getDefault().postSticky(CategoryEvent2.make(EventConstant.EVENT_CATEGORY_CHOOSE2, 3));
            }
        });
    }

    @Override
    protected void onLazyLoad() {
        viewModel.groupBuyClassList(mItemType);
    }

    @Override
    public void onVisible() {
        super.onVisible();
        if (mItemType == 3) {
            if (typeTwoEntityList == null || typeTwoEntityList.size() == 0) {
                viewModel.groupBuyClassList(mItemType);
                return;
            }
        } else {
            if (typeEntityList == null || typeEntityList.size() == 0) {
                viewModel.groupBuyClassList(mItemType);
                return;
            }
        }
        loadData(false);
    }

    private void loadData(boolean isLoadMore) {
        mItemType = getItemType();
        if (mItemType == -1) {
            return;
        }
        page = isLoadMore ? page + 1 : 1;
        switch (mItemType) {
//            case 0://即采
//                if (null!=typeEntity){
//                    viewModel.getJiCaiList(1, isLoadMore, typeEntity.getNo().isEmpty() ? 0 : Integer.valueOf(typeEntity.getNo()), page, getIsRefresh());
//                }
//                break;
//            case 1://汇总
//                if (null!=typeEntity) {
//                    viewModel.getJiCaiList(2, isLoadMore, typeEntity.getNo().isEmpty() ? 0 : Integer.valueOf(typeEntity.getNo()), page, getIsRefresh());
//                }
//                break;
            case 2://专项
                if (null != typeEntity) {
                    viewModel.getJiCaiList(3, isLoadMore, typeEntity.getNo().isEmpty() ? 0 : Integer.valueOf(typeEntity.getNo()), page, getIsRefresh(), "");
                }
                break;
            case 3://需方发起
                if (null != typeEntity) {
                    if (null != typeTwoEntity) {
                        viewModel.getJiCaiList(0, isLoadMore, typeEntity.getNo().isEmpty() ? 0 : Integer.valueOf(typeEntity.getNo()), page, getIsRefresh(), typeTwoEntity.getNo());
                    } else {
                        viewModel.getJiCaiList(0, isLoadMore, typeEntity.getNo().isEmpty() ? 0 : Integer.valueOf(typeEntity.getNo()), page, getIsRefresh(), "");
                    }
                }
                break;
            case 4://供方发起
                if (null != typeEntity) {
                    viewModel.getFactoryCentralizedList(isLoadMore, page, typeEntity.getNo().isEmpty() ? "" : typeEntity.getNo());
                }
                break;
            default:
                throw new IllegalStateException("Unexpected value: " + mItemType);
        }
    }

    public void showList(List<CentralizedMiningEntity> list, boolean isLoadMore) {
        if (isLoadMore) {
            if (list != null && list.size() > 0) {
                centralizedMining4Adapter.addDatas(list);
                binding.recycler.setVisibility(View.VISIBLE);
                binding.emptyView.setVisibility(View.GONE);
                if (list.size() < 3) {
                    binding.lineMore.setVisibility(View.VISIBLE);
                    binding.tvMore.setVisibility(View.GONE);
                } else {
                    binding.lineMore.setVisibility(View.VISIBLE);
                }

            } else {
                binding.lineMore.setVisibility(View.VISIBLE);
                binding.tvMore.setVisibility(View.GONE);
                binding.recycler.setVisibility(View.GONE);
                binding.emptyView.setVisibility(View.VISIBLE);
            }
        } else {
            if (list != null && list.size() > 0) {
                binding.lineMore.setVisibility(View.VISIBLE);
                binding.recycler.setVisibility(View.VISIBLE);
                binding.emptyView.setVisibility(View.GONE);
                if (list.size() < 3) {
                    binding.lineMore.setVisibility(View.VISIBLE);
                    binding.tvMore.setVisibility(View.GONE);
                } else {
                    binding.lineMore.setVisibility(View.VISIBLE);
                }
            } else {
                binding.lineMore.setVisibility(View.GONE);
                binding.emptyView.setVisibility(View.VISIBLE);
                binding.recycler.setVisibility(View.GONE);
            }
            centralizedMining4Adapter.setDatas(list);
        }
        binding.recycler.setAdapter(centralizedMining4Adapter);
    }

    public void showFactoryList(List<FactoryCentralizedItemEntity> list, boolean isLoadMore) {
        if (isLoadMore) {
            if (list != null && list.size() > 0) {
                factoryCentralizedListItemAdapter2.addDatas(list);
                binding.recycler.setVisibility(View.VISIBLE);
                binding.emptyView.setVisibility(View.GONE);
                if (list.size() < 3) {
                    binding.lineMore.setVisibility(View.VISIBLE);
                    binding.tvMore.setVisibility(View.GONE);
                } else {
                    binding.lineMore.setVisibility(View.VISIBLE);
                }

            } else {
                binding.lineMore.setVisibility(View.VISIBLE);
                binding.tvMore.setVisibility(View.GONE);
                binding.recycler.setVisibility(View.GONE);
                binding.emptyView.setVisibility(View.VISIBLE);
            }
        } else {
            if (list != null && list.size() > 0) {
                binding.lineMore.setVisibility(View.VISIBLE);
                binding.recycler.setVisibility(View.VISIBLE);
                binding.emptyView.setVisibility(View.GONE);
                if (list.size() < 3) {
                    binding.lineMore.setVisibility(View.VISIBLE);
                    binding.tvMore.setVisibility(View.GONE);
                } else {
                    binding.lineMore.setVisibility(View.VISIBLE);
                }
            } else {
                binding.lineMore.setVisibility(View.GONE);
                binding.emptyView.setVisibility(View.VISIBLE);
                binding.recycler.setVisibility(View.GONE);
            }
            factoryCentralizedListItemAdapter2.setDatas(list);
        }
        binding.recycler.setAdapter(factoryCentralizedListItemAdapter2);
    }

    public void showOption1View2(List<PickSelectEntity> datas) {
//        if (datas == null || datas.size() == 0) {
//            binding.emptyView.setVisibility(View.VISIBLE);
//            return;
//        }
        binding.tabLayout.clearOnTabSelectedListeners();
        binding.tabLayout.removeAllTabs();
        if (!TextUtils.equals(datas.get(0).getProjectscale(), "全部"))
            datas.add(0, new PickSelectEntity("", "全部"));
        for (int i = 0; i < datas.size(); i++) {
            binding.tabLayout.addTab(binding.tabLayout.newTab().setText(datas.get(i).getPickerViewText()));
        }
        binding.tabLayout.scrollTo(0, 0);
        typeEntityList = datas;
        typeEntity = datas.get(0);
        binding.tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                if (centralizedMining4Adapter != null && centralizedMining4Adapter.datas.size() > 0) {
                    centralizedMining4Adapter.datas.clear();
                }
                if (factoryCentralizedListItemAdapter2 != null && factoryCentralizedListItemAdapter2.getDatas().size() > 0) {
                    factoryCentralizedListItemAdapter2.getDatas().clear();
                }
                page = 1;
                typeEntity = datas.get(tab.getPosition());
                typeTwoEntity = null;
                if (mItemType == 3) {   //需方发起
                    if (tab.getPosition() != 0) {
                        binding.layoutType.setVisibility(View.VISIBLE);
                        viewModel.getTenderGoodsCategoryAllList("0", tab.getPosition());
                        return;
                    } else {
                        binding.layoutType.setVisibility(View.GONE);
                    }
                } else {
                    binding.layoutType.setVisibility(View.GONE);
                }
                loadData(false);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        loadData(false);

    }

    public void showOption2View3(List<PickSelectEntity> datas) {
        binding.tabLayoutType.clearOnTabSelectedListeners();
        binding.tabLayoutType.removeAllTabs();
        if (!TextUtils.equals(datas.get(0).getProjectscale(), "全部"))
            datas.add(0, new PickSelectEntity("", "全部"));
        for (int i = 0; i < datas.size(); i++) {
            binding.tabLayoutType.addTab(binding.tabLayoutType.newTab().setText(datas.get(i).getPickerViewText()));
        }
        binding.tabLayoutType.scrollTo(0, 0);
        typeTwoEntityList = datas;
        binding.tabLayoutType.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                typeTwoEntity = datas.get(tab.getPosition());
                loadData(false);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        loadData(false);

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
